import java.io.*;
import flex.messaging.io.SerializationContext;
import flex.messaging.io.amf.Amf3Output;

public class AMF3OutputStream extends DataOutputStream {
	private SerializationContext serializationContext = new SerializationContext();
	private Amf3Output amfoutput = new Amf3Output(serializationContext);
	private ByteArrayOutputStream bos = new ByteArrayOutputStream();
	private DataOutputStream dos = new DataOutputStream(bos);
	
	public AMF3OutputStream(OutputStream output)
	{
		super(output);
		amfoutput.setOutputStream(dos);
	}
	
	public synchronized void writeAMF3Object(Object obj) throws IOException
	{
		amfoutput.reset();
		bos.reset();
		amfoutput.writeObject(obj);
		amfoutput.flush();
		bos.flush();
		
		byte[] b = bos.toByteArray();
		
		this.writeInt(b.length);
		this.write(b);
	}
}
